package edu.depaul.se.courseregistration.jpa.schedule;

import edu.depaul.se.courseregistration.jpa.courses.Section;
import edu.depaul.se.courseregistration.jpa.user.Person;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;


/**
 *
 * @author lmarco, gkhuram
 */

@Entity
@Table(name = "SCHEDULE")
@NamedQueries({
    @NamedQuery(name = Schedule.CLASS_QUERY_BY_SECTION_ID, query = "SELECT s FROM Schedule s WHERE s.sectionId = :" + Schedule.SECTION_ID_PARAMETER_NAME), //Find by sectionID
    @NamedQuery(name = Schedule.CLASS_QUERY_BY_PERSON_ID, query = "SELECT s from Schedule s WHERE s.personID = :" + Schedule.PERSON_ID_PARAMETER_NAME), //Find personId
    @NamedQuery(name = Schedule.CLASS_QUERY_BY_SECTION_AND_PERSON, query = "SELECT s FROM Schedule s WHERE s.sectionId = :" + Schedule.SECTION_ID_PARAMETER_NAME + " AND  s.personID = :" + Schedule.PERSON_ID_PARAMETER_NAME), //Find by both
    @NamedQuery(name = Schedule.CLASS_QUERY_BY_STUDENT, query = "SELECT s FROM EnrolledClass s WHERE s.sectionId = :" + Schedule.SECTION_ID_PARAMETER_NAME), //Find by student
    @NamedQuery(name = Schedule.CLASS_QUERY_BY_PROFESSOR, query = "SELECT s FROM CourseBeingTaught s WHERE s.sectionId = :" + Schedule.SECTION_ID_PARAMETER_NAME), //Find by student
    @NamedQuery(name = Schedule.CLASS_QUERY_GET_ALL, query = "SELECT s FROM Schedule s") //Find all

})

@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="USR_TYPE")
@XmlSeeAlso({CourseBeingTaught.class, EnrolledClass.class})
public abstract class Schedule implements Serializable {
    
    private static final long serialVersionUID = 1L;
    public static final String CLASS_QUERY_BY_SECTION_ID = "Schedule.findBySectionId";
    public static final String CLASS_QUERY_GET_ALL = "Schedule.findAll";
    public static final String CLASS_QUERY_BY_STUDENT = "Schedule.findByStudent";
    public static final String CLASS_QUERY_BY_PROFESSOR = "Schedule.findByProfessor";
    public static final String CLASS_QUERY_BY_SECTION_AND_PERSON = "Schedule.findBySectionIdAndPersonId";
    public static final String CLASS_QUERY_BY_PERSON_ID = "Schedule.findByPersonId";
    public static final String SECTION_ID_PARAMETER_NAME = "sectionId";
    public static final String PERSON_ID_PARAMETER_NAME = "personID";


    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "SCHEDULE_ID")
    private Integer scheduleID;

    public Integer getScheduleID() {
        return scheduleID;
    }

    public void setScheduleID(Integer scheduleID) {
        this.scheduleID = scheduleID;
    }
    
    //@Column(name = "PERSON_ID")
    @ManyToOne(optional=false)
    private Person personID;
    
    //@Column(name = "SECTION_ID")
    @OneToOne(optional=false)
      private Section sectionId;
    
   
    @XmlTransient
    public Section getSection() {
        return sectionId;
    }

    public void setSection(Section section) {
        this.sectionId = section;
    }
    @XmlTransient
    public Person getPersonID() {
        return personID;
    }

    public void setPersonID(Person personID) {
        this.personID = personID;
    } 
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("----------------------------------------------------------------------------------------");     
        sb.append(sectionId.getCourseNumber());
        sb.append("\n");
        sb.append("Course Section: ").append(sectionId.getSectionID());
        sb.append("\n");
        sb.append("Section Price: " + "$").append(sectionId.getSectionPrice());
        sb.append("\n");
        sb.append("Pesron Name: ").append(personID.getFirstName()).append(" ").append(personID.getLastName());
        sb.append("\n");
        sb.append(new Date());
        sb.append("\n");
        sb.append("----------------------------------------------------------------------------------------");     
        return sb.toString();
    }
}
